<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/10
 * Time: 11:27
 */
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use app\admin\model\Banner as BannerModel;
class Start extends Common
{
    /*
     * 全屏列表
     */
    public function all()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $count = Db::name('app_start')->where('type',1)->count();
            $data = Db::name('app_start')
                ->limit($startrow, $pagesize)
                ->where('type',1)
                ->order('id','desc')
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));

        } else {
            return view();
        }
    }
    /*
     * 修改页面的展示
     */
    public function editAll()
    {
        //获取id
        $id = $_GET['id'];
        Session::set('id', $id);
        //查询数据库
        $data = Db::name('app_start')
            ->where('id', $id)
            ->find();
        if ($data !== false) {
            $this->assign('data', $data);
        }
        return $this->fetch();
    }
    /*
     * 修改
     */
    public function editXiu()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            if (!$post['banner_img']) {
                return ['code' => 1, 'msg' => '请上传轮播图'];
            }
            //存入到数组中
            $data = [
                'app_img' => $post['banner_img'],
                'create_time' => time(),
            ];

            $res = Db::name('app_start')->where('id', Session::get('id'))->update($data);

            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 2];
            }
        }
    }
    /*
     * 普通屏列表
     */
    public function common()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $count = Db::name('app_start')->where('type',2)->count();
            $data = Db::name('app_start')
                ->limit($startrow, $pagesize)
                ->where('type',2)
                ->order('id','desc')
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));

        } else {
            return view();
        }
    }

    /*
     * xiugai
     */
    public function editCommon()
    {
        //获取id
        $id = $_GET['id'];
        Session::set('id', $id);
        //查询数据库
        $data = Db::name('app_start')
            ->where('id', $id)
            ->find();
        if ($data !== false) {
            $this->assign('data', $data);
        }
        return $this->fetch();
    }
}